#ifndef SIMPLEDNS_H_
#define SIMPLEDNS_H_

#define E_NS_NOT_FOUND -1
#define E_HOSTNAME_TOO_LONG -2
#define E_NO_RECORD_FOUND -3
#define E_NO_HANDLES_AVAILABLE -4
#define E_RECV_ERROR -5
/**
 * Struct for storing IP addresses and port numbers.
 */
typedef struct {
	/**
	 * The IP address, as 4 bytes: {192, 168, 1, 1}
	 */
	char addr[4];

	/*
	 * The port number, as 2 bytes: can be any value from 0 to 2^16-1
	 */
	char port[2];
} addr_record;

/*
 * Resolve a host name using nameservers listed in a file.
 *
 * @param nsfile the ns file path
 * @param host_name the host name to resolve
 * @param result the address structure to fill
 * @return 0 if success, >0 otherwise
 */

int resolve_from_file(const char * nsfile, const char * host_name, addr_record * result);

/*
 * Register an IP Address/port to a host name.
 *
 * @param nsfile the ns file path
 * @param host_name the host name to resolve
 * @param result the address structure to fill
 * @return 0 if success, >0 otherwise
 */
int register_to_file(const char * nsfile, const char * host_name, addr_record * record);

/*
 * Allocate a handle for a DNS server, identified by a host name/fqdn/IP address and port number. If there is
 * an error allocating the handle, returns -1. Requests are made over UDP, so the semantic of an "open connection"
 * does not make sense for this library.
 *
 * @param ns_name The nameserver's name -- can be hostname (if on LAN), FQDN, or IP address.
 * @param ns_port The nameserver's UDP port.
 * @return The handle on success, -1 on error.
 */
int open_ns(char * ns_name, int ns_port);

/*
 * Resolve a name into an addr_record. Sends a UPD request to server identified by dnsp.
 *
 * @param dnsp The nameserver handle to request to
 * @param host_name The host name to resolve
 * @param result The space to store results in.
 * @return 0 if success, <0 if failure.
 */
int resolve_name(int dnsp, const char * host_name, addr_record * result);

/*
 * Register a name with a nameserver.
 *
 * @param dnsp The nameserver handle to request to
 * @param host_name The host name to register
 * @param new_record The new server's address information.
 * @return 0 if success, <0 if failure.
 */
int register_name(int dnsp, const char * host_name, addr_record * new_record);

/**
 * Release a nameserver handle
 *
 * @param dnsp The handle to release
 * @return 0 if success, <0 if failure
 */
int close_dns(int dnsp);

#endif
